﻿using Furion.Extras.Admin.NET;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Admin.NET.Application.Entity;

namespace Admin.NET.Application
{
    /// <summary>
    /// 测试A服务
    /// </summary>
    [ApiDescriptionSettings("自己的业务", Name = "TestA", Order = 100)]
    public class TestAService : ITestAService, IDynamicApiController, ITransient
    {
        private readonly IRepository<TestA> _rep;

        public TestAService(
            IRepository<TestA> rep
        )
        {
            _rep = rep;
        }

        /// <summary>
        /// 分页查询测试A
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/TestA/page")]
        public async Task<dynamic> Page([FromQuery] TestAInput input)
        {
            var entities = await _rep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.Text1), u => EF.Functions.Like(u.Text1, $"%{input.Text1.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Text2), u => EF.Functions.Like(u.Text2, $"%{input.Text2.Trim()}%"))
                                     .Where(input.Time1 !=null  ,u=>u.Time1 == input.Time1)
                                     .OrderBy(PageInputOrder.OrderBuilder<TestAInput>(input))
                                     .ToPagedListAsync(input.PageNo, input.PageSize);
            var result = XnPageResult<TestA>.PageResult<TestADto>(entities);
            await DtoMapper(result.Rows);
            return result;
        }

        /// <summary>
        /// 增加测试A
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/TestA/add")]
        public async Task Add(AddTestAInput input)
        {
            var entity = input.Adapt<TestA>();
            await entity.InsertAsync();
        }

        /// <summary>
        /// 删除测试A
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/TestA/delete")]
        public async Task Delete(DeleteTestAInput input)
        {
            var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await entity.DeleteAsync();
        }

        /// <summary>
        /// 更新测试A
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/TestA/edit")]
        public async Task Update(UpdateTestAInput input)
        {
            var entity = input.Adapt<TestA>();
            await entity.UpdateAsync(true);
        }

        /// <summary>
        /// 获取测试A
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/TestA/detail")]
        public async Task<dynamic> Get([FromQuery] QueryeTestAInput input)
        {
            return await _rep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id);
        }

        /// <summary>
        /// 获取测试A列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/TestA/list")]
        public async Task<dynamic> List([FromQuery] TestAInput input)
        {
            return await _rep.DetachedEntities.ToListAsync();
        }    

        private async Task DtoMapper(ICollection<TestADto> rows)
        {
            foreach (var item in rows)
            {
            }
        }
    }
}
